<?php

class Mithos_Auth {
    private function __construct() {
        
    }

    private function __clone() {
        
    }

    public static function user() {
        $auth = new Zend_Session_Namespace('Auth');
        if (self::loggedIn()) {
            return $auth->username;
        }
    }

    public static function loggedIn() {
        $auth = new Zend_Session_Namespace('Auth');
        return isset($auth->username);
    }

    public static function login($username, $password) {
        if (!self::check($username, $password)) {
            return false;
        } else {
            $auth = new Zend_Session_Namespace('Auth');
            $auth->username = $username;
            return true;
        }
        return false;
    }

    public static function logout($url = null) {
        $auth = new Zend_Session_Namespace('Auth');
        unset($auth->username);
        Zend_Session::destroy();
        if ($url !== null) {
            redirect($url);
        }
    }

    private static function check($username, $password) {
        $result = Mithos_Mssql::getInstance()->fetch('SELECT COUNT(1) AS total 
            FROM MEMB_INFO WHERE memb___id = :username[string] AND 
            memb__pwd = :password[string]
        ', array('username' => $username, 'password' => $password));
        return $result['total'] == 1;
    }
}

?>